package dataStore;

import java.io.IOException;
import java.net.InetAddress;
import java.sql.SQLException;

import tool.FileTurn;
import tool.GetProper;

import dataStruct.ChunkInfo;
import dataStruct.DataStoreTask;

public class UploadToFtp {
	public UploadToFtp(SlaverDataStoreApiInterface masterDSApi,
			FtpServerApiInterface ftpServerApi) {
		UploadToFtp.masterDSApi = masterDSApi;
		UploadToFtp.ftpServerApi = ftpServerApi;
	}

	public Boolean upload_to_ftp(String dataStoreTaskId) throws IOException,
			SQLException {
		// TODO Auto-generated method stub
		DataStoreTask dataStoreTask = masterDSApi.get_data_store_task_bag(
				dataStoreTaskId, new Integer(InetAddress.getLocalHost()
						.hashCode()));
		System.out
				.println(String
						.format("dataStoreTask.offset = %d, dataStoreTask.size=%d, dataStoreTask.chunkId=%s(UploadToFtp.java)",
								dataStoreTask.offset, dataStoreTask.size,
								dataStoreTask.chunkId));

		String slaverFilePath = GetProper.get_properties("conf/config.ini")
				.getProperty("slaverWorkplace");
		slaverFilePath += "/" + dataStoreTaskId;
		String ftpFilePath = dataStoreTask.ftpServerInfo.ftpServerPath;

		byte[] chunkContent = FileTurn.file_to_byte(slaverFilePath);

		// debug
		System.out.println(String.format(
				"chunkContent = %s, lenth = %d(UploadToFtp.java)",
				chunkContent.toString(), chunkContent.length));

		ChunkInfo chunkInfo = new ChunkInfo();
		chunkInfo.chunkContent = chunkContent;
		chunkInfo.chunkWordcountMap = null;
		chunkInfo.offset = dataStoreTask.offset;
		chunkInfo.size = dataStoreTask.size;

		if (ftpServerApi.upload_chunk(chunkInfo, ftpFilePath,
				dataStoreTask.totalSize) == false) {
			masterDSApi.fail_upload_chunk(dataStoreTaskId);
			System.out.println("upload chunk error (UploadToFtp.java)");
			return false;
		}

		// debug
		System.out
				.println(String
						.format("slaverFilePath=%s,offset = %d, size = %d has finished upload(UploadToFtp.java)",
								slaverFilePath, dataStoreTask.offset,
								dataStoreTask.size));
		masterDSApi.finish_upload_chunk(dataStoreTaskId);
		return true;
	}

	public Boolean wordcount_upload_to_ftp(String dataStoreTaskId)
			throws IOException, SQLException {
		// TODO Auto-generated method stub
		DataStoreTask dataStoreTask = masterDSApi.get_data_store_task_bag(
				dataStoreTaskId, new Integer(InetAddress.getLocalHost()
						.hashCode()));
		System.out
				.println(String
						.format("dataStoreTask.offset = %d, dataStoreTask.size=%d, dataStoreTask.chunkId=%s(UploadToFtp.java)",
								dataStoreTask.offset, dataStoreTask.size,
								dataStoreTask.chunkId));

		String slaverFilePath = GetProper.get_properties("conf/config.ini")
				.getProperty("slaverWorkplace");
		slaverFilePath += "/" + dataStoreTaskId;
		String ftpFilePath = dataStoreTask.ftpServerInfo.ftpServerPath;

		ChunkInfo chunkInfo = new ChunkInfo();
		chunkInfo.chunkContent = null;
		chunkInfo.chunkWordcountMap = FileTurn.wordcount_file_to_byte(slaverFilePath);
		chunkInfo.offset = dataStoreTask.offset;
		chunkInfo.size = dataStoreTask.size;

		if (ftpServerApi.upload_chunk(chunkInfo, ftpFilePath,
				dataStoreTask.totalSize) == false) {
			masterDSApi.fail_upload_chunk(dataStoreTaskId);
			System.out.println("upload chunk error (UploadToFtp.java)");
			return false;
		}

		// debug
		System.out
				.println(String
						.format("slaverFilePath=%s,offset = %d, size = %d has finished upload(UploadToFtp.java)",
								slaverFilePath, dataStoreTask.offset,
								dataStoreTask.size));
		masterDSApi.finish_upload_chunk(dataStoreTaskId);
		return true;
	}

	private static SlaverDataStoreApiInterface masterDSApi;
	private static FtpServerApiInterface ftpServerApi;

}
